#ifndef __HDVB_EITDB_H
#define __HDVB_EITDB_H
/*
    libhdvbd - Library for building sophisticated DVB applications
    Copyright (C) 2012 Tony Houghton <h@realh.co.uk>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free dSoftware
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* EIT database */

#include <glib.h>

#include <time.h>

#include <sqlite3.h>

#include <hdvb/dbcommon.h>

G_BEGIN_DECLS

/* Underlying structure and queries/statements */

#define HDVB_EIT_DB_VERSION 1

enum {
    HDVB_EITDB_COLUMN_EVENTS_SERVICE_ID = 0,
    HDVB_EITDB_COLUMN_EVENTS_TS_ID,
    HDVB_EITDB_COLUMN_EVENTS_NETWORK_ID,
    HDVB_EITDB_COLUMN_EVENTS_EVENT_ID,
    HDVB_EITDB_COLUMN_EVENTS_START_TIME,
    HDVB_EITDB_COLUMN_EVENTS_DURATION,
    HDVB_EITDB_COLUMN_EVENTS_SCRAMBLED,
    
    HDVB_EITDB_EVENTS_N_COLUMNS
};

enum {
    HDVB_EITDB_COLUMN_SHORT_DESCRIPTORS_SERVICE_ID = 0,
    HDVB_EITDB_COLUMN_SHORT_DESCRIPTORS_EVENT_ID,
    HDVB_EITDB_COLUMN_SHORT_DESCRIPTORS_LANGUAGE,
    HDVB_EITDB_COLUMN_SHORT_DESCRIPTORS_EVENT_NAME,
    HDVB_EITDB_COLUMN_SHORT_DESCRIPTORS_EVENT_TEXT,
    
    HDVB_EITDB_SHORT_DESCRIPTORS_N_COLUMNS
};

enum {
    HDVB_EITDB_COLUMN_TIME_SHIFTED_EVENTS_SERVICE_ID = 0,
    HDVB_EITDB_COLUMN_TIME_SHIFTED_EVENTS_EVENT_ID,
    HDVB_EITDB_COLUMN_TIME_SHIFTED_EVENTS_REF_SERVICE_ID = 0,
    HDVB_EITDB_COLUMN_TIME_SHIFTED_EVENTS_REF_EVENT_ID,
    
    HDVB_EITDB_TIME_SHIFTED_EVENTS_N_COLUMNS
};

/* handle can be used to pass in a decoder string:
 * "dvb"        : (default if NULL), standard DVB encodings
 * "utf8"       : all strings are UTF-8
 * "freesat"    : (may contain) Huffman-encoded strings as well as dvb
 * "freeviewhd" : (may contain) Huffman-encoded strings as well as dvb
 */
void
hdvb_eit_db_create_schemas(sqlite3 *sql, gpointer handle);

sqlite3_stmt *
hdvb_eit_db_statement_insert_event(sqlite3 *sql);

sqlite3_stmt *
hdvb_eit_db_statement_insert_short_descriptor(sqlite3 *sql);

sqlite3_stmt *
hdvb_eit_db_statement_insert_time_shifted_event(sqlite3 *sql);

/* For querying the database */

typedef struct {
    int service_id;
    int event_id;
    time_t start_time;
    time_t duration;
    gboolean scrambled;
    time_t stamp;
    char *event_name;
    char *event_text;
    char lang[4];
} HdvbEitDbEventDetails;

HdvbEitDbEventDetails *
hdvb_eit_db_event_details_get(sqlite3 *sql, int service_id, int event_id);

void
hdvb_eit_db_event_details_free(HdvbEitDbEventDetails *details);

/* Returns a list of HdvbEitDbEventDetails sorted by start time and overlapping
 * events trimmed. If full_details is FALSE the event_text and lang fields
 * won't be filled in. If  start_time and end_time are != 0, events are bounded
 * by those times.
 */
GList *
hdvb_eit_db_list_events(sqlite3 *sql, int service_id,
        gboolean full_details, time_t start_time, time_t end_time);

void
hdvb_eit_db_event_details_free_list(GList *list);

/* Removes all events which *end* before tm */
void
hdvb_eit_db_remove_old_events(sqlite3 *sql, time_t tm);

/* The standard function for decoding a string from the EIT */
void
hdvb_eit_db_str_decoder(sqlite3_context *sctx, int n, sqlite3_value **argv);

/* Sets up an sqlite3 function called hdvb_decode to convert the binary
 * blobs from EIT event names and descriptions to UTF-8
 */
gboolean
hdvb_eit_db_setup_decoder(sqlite3 *sql);

typedef char *(*HdvbEitDbDecoder)(const guint8 *blob, int len);

HdvbEitDbDecoder
hdvb_eit_db_get_decoder(sqlite3 *sql);

G_END_DECLS

#endif /* __HDVB_EITDB_H */
